<?php

namespace App\Console\WxtjCommands;

use App\Services\WxMp\WxMpListService;
use App\Services\Wxtj\WxtjUpstream;
use Illuminate\Console\Command;

/**
 * 消息分析数据统计：消息分送分时数据
 * @package App\Console\WxtjCommands
 */
class UpstreamMsgHour extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wxtj-upstream:msg-hour
            {beginDate? : 获取数据的起始日期.}
            {endDate? : 获取数据的结束日期.}
            {appid? : 指定微信应用}
            {--all : 是否拉取所有数据(默认全年)}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '[公众号]消息分析数据统计：消息分送分时数据';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $appid =  $this->argument('appid');
        if ($appid){
            $this->handleByAppid($appid);
        }else{
            $appList = (new WxMpListService())->getValidMpListByType(WxMpListService::TYPE_WECHAT);
            if (!empty($appList)){
                foreach ($appList as $app){
                    $this->handleByAppid($app['appid']);
                }
            }
        }
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    private function handleByAppid($appid)
    {
        $wxtjUpstream = new WxtjUpstream($appid);
        $isAll = $this->option('all');
        if ($isAll){
            $wxtjUpstream->pullAllMsgHour();
        }else{
            $beginDate = $this->argument('beginDate');
            $endDate = $this->argument('endDate');
            $wxtjUpstream->pullMsgHour($beginDate, $endDate);
        }
    }
}
